package haoyou.csnf.manager.module.pm.controller.admin.rulesclasshour;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import haoyou.csnf.manager.framework.common.pojo.PageParam;
import haoyou.csnf.manager.framework.common.pojo.PageResult;
import haoyou.csnf.manager.framework.common.pojo.CommonResult;
import haoyou.csnf.manager.framework.common.util.object.BeanUtils;
import static haoyou.csnf.manager.framework.common.pojo.CommonResult.success;

import haoyou.csnf.manager.framework.excel.core.util.ExcelUtils;

import haoyou.csnf.manager.framework.apilog.core.annotation.ApiAccessLog;
import static haoyou.csnf.manager.framework.apilog.core.enums.OperateTypeEnum.*;

import haoyou.csnf.manager.module.pm.controller.admin.rulesclasshour.vo.*;
import haoyou.csnf.manager.module.pm.dal.dataobject.rulesclasshour.RulesClasshourDO;
import haoyou.csnf.manager.module.pm.service.rulesclasshour.RulesClasshourService;

@Tag(name = "管理后台 - 课时费规则")
@RestController
@RequestMapping("/pm/rules-classhour")
@Validated
public class RulesClasshourController {

    @Resource
    private RulesClasshourService rulesClasshourService;

    @PostMapping("/create")
    @Operation(summary = "创建课时费规则")
    @PreAuthorize("@ss.hasPermission('pm:rules-classhour:create')")
    public CommonResult<Integer> createRulesClasshour(@Valid @RequestBody RulesClasshourSaveReqVO createReqVO) {
        return success(rulesClasshourService.createRulesClasshour(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课时费规则")
    @PreAuthorize("@ss.hasPermission('pm:rules-classhour:update')")
    public CommonResult<Boolean> updateRulesClasshour(@Valid @RequestBody RulesClasshourSaveReqVO updateReqVO) {
        rulesClasshourService.updateRulesClasshour(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除课时费规则")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pm:rules-classhour:delete')")
    public CommonResult<Boolean> deleteRulesClasshour(@RequestParam("id") Integer id) {
        rulesClasshourService.deleteRulesClasshour(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除课时费规则")
                @PreAuthorize("@ss.hasPermission('pm:rules-classhour:delete')")
    public CommonResult<Boolean> deleteRulesClasshourList(@RequestParam("ids") List<Integer> ids) {
        rulesClasshourService.deleteRulesClasshourListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得课时费规则")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pm:rules-classhour:query')")
    public CommonResult<RulesClasshourRespVO> getRulesClasshour(@RequestParam("id") Integer id) {
        RulesClasshourDO rulesClasshour = rulesClasshourService.getRulesClasshour(id);
        return success(BeanUtils.toBean(rulesClasshour, RulesClasshourRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得课时费规则分页")
    @PreAuthorize("@ss.hasPermission('pm:rules-classhour:query')")
    public CommonResult<PageResult<RulesClasshourRespVO>> getRulesClasshourPage(@Valid RulesClasshourPageReqVO pageReqVO) {
        PageResult<RulesClasshourDO> pageResult = rulesClasshourService.getRulesClasshourPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RulesClasshourRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课时费规则 Excel")
    @PreAuthorize("@ss.hasPermission('pm:rules-classhour:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRulesClasshourExcel(@Valid RulesClasshourPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RulesClasshourDO> list = rulesClasshourService.getRulesClasshourPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "课时费规则.xls", "数据", RulesClasshourRespVO.class,
                        BeanUtils.toBean(list, RulesClasshourRespVO.class));
    }

}